package cn.com.homeyoda.aimate.aihealth.ui.bluetooth_guide;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.transformers.Transformer;

import java.util.ArrayList;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBannerBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityBluetoothGuideBinding;
import cn.com.homeyoda.aimate.aihealth.ui.protocol.ProtocolMainActivity;

public class BluetoothGuideActivity extends BaseActivity<ActivityBluetoothGuideBinding, BluetoothGuideViewModel> implements View.OnClickListener {

    private final ArrayList<KePuBannerBean> bannerList = new ArrayList<>();

    public static void start(Context context) {
        context.startActivity(new Intent(context, BluetoothGuideActivity.class));
    }

    @Override
    public BluetoothGuideViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(BluetoothGuideViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_bluetooth_guide;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getBanner();
        initView();
    }

    private void initView() {
        binding.titleBar.ivBack.setOnClickListener(this);
        binding.btnBtMatching.setOnClickListener(this);
        binding.llReport.setOnClickListener(this);
        binding.titleBar.tvTitle.setText(getString(R.string.home_device));
    }

    private void setBannerView() {
        // XBanner banner = binding.protocolBanner;
        binding.btMatchingBanner.setBannerData(bannerList);
        binding.btMatchingBanner.loadImage((banner12, model, view, position) -> {
            // KePuBannerBean bannerBean = (KePuBannerBean) model;
            Glide.with(this)
                    // .load(bannerBean.getXBannerUrl())
                    .load(R.mipmap.test1)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(16)))
                    .into((ImageView) view);
        });
        binding.btMatchingBanner.setPageTransformer(Transformer.Default);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.bannerData.observe(this, bannerList -> {
            this.bannerList.clear();
            this.bannerList.addAll(bannerList);
            setBannerView();
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.btn_bt_matching) {
            finish();
        } else if (id == R.id.ll_report) {// finish();
            ProtocolMainActivity.start(this);
        }
    }
}